
//
//  WBNetwork+Extension.swift
//  SinaWebo
//
//  Created by 亚杏 on 2017/5/16.
//  Copyright © 2017年 oms. All rights reserved.
//

import Foundation

//封闭新浪微博的网络请求方法
extension WBNetworkManager{

    func statusList(since_id: Int64 = 0, max_id: Int64 = 0 , completion: @escaping (_ list: [[String : AnyObject]]?, _ isSuccess: Bool)->()) {
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        //Swift中int64不能转成anyobject
        let params = ["since_id": "\(since_id)","max_id": "\(max_id > 0 ? max_id - 1 : 0)"]

        tokenRequest(URLString: urlStr, parameters: params as [String : AnyObject]) { (json: AnyObject?,isSuccess: Bool) in
            //获取json中的 statuses 字典数组
            let result = json?["statuses"] as? [[String : AnyObject]]
            
            completion(result, isSuccess)

        }
    }


    
}

//MARK: Oauth相关方法
extension WBNetworkManager{

    func loadaccessToken(code: String, completion: @escaping (_ isSuccess: Bool) -> ()) {

        let params = ["client_id": WBAppKey,"client_secret": WBAppSecret,"grant_type": "authorization_code","code": code,"redirect_uri": WBRedirectURL]

        //发起网络请求
        request(Method: .POST, URLString: Token_URL, parameters: params as [String : AnyObject]) { (json, isSuccess) in
            //字典设置userAccount的属性
            //为拿token
            self.userAccount.yy_modelSet(with: (json as? [String : AnyObject]) ?? [:])
//            print(self.userAccount)
            
            //加载当前用户信息
            self.loadUserInfo(completion: { (dict) in
                print(dict)
                
                //将dict里面数据取出,主要是昵称和头像
                self.userAccount.yy_modelSet(with: dict)
                print(self.userAccount)
                self.userAccount.saveAccount()

                completion(isSuccess)

            })
        }
    }

}

//MARK：发布微博
extension WBNetworkManager {

    ///   - text: 要发布的文本
    ///   - image: 要上传的图像，为nil时，是纯文本
    ///   - completion: 完成回调
    func postMessage(text: String, image: UIImage?, completion: @escaping (_ result: [String: AnyObject]?, _ isSuccess: Bool)->()) -> () {
        //1. url
        let urlStr: String

        if image == nil {
            //纯文本
            urlStr = "https://api.weibo.com/2/statuses/update.json"
        } else {
            //有图像
            urlStr = "https://upload.api.weibo.com/2/statuses/upload.json"
        }

        //2.参数
        let params = ["status": text]

        //3.如果上传图像，设置name和data
        var name: String?
        var data: Data?

        if image != nil {
            name = "pic"
            data = UIImagePNGRepresentation(image!)
        }

        //4.发起请求
        tokenRequest(Method: .POST, URLString: urlStr, parameters: params as [String : AnyObject], name: name, data: data) { (json, isSuccess) in
            completion(json as? [String : AnyObject], isSuccess)
        }
    }
}

//MARK: 用户信息
extension WBNetworkManager{

    //加载当前用户信息
    func loadUserInfo(completion: @escaping (_ dict: [String : AnyObject]) -> () ) {
        
        guard let uid = userAccount.uid else {
            return
        }
        
        let params = ["uid" : uid]

        //发起网络请求
        tokenRequest(URLString: userInfo_URL, parameters: params as [String : AnyObject]?) { (json, isSuccess) in
//            print(json ?? "")
            
            completion((json as? [String : AnyObject]) ?? [:])
        }
    
    }
    
}
